package git.soulbgm.common.session;

import git.soulbgm.pojo.PrincipalUser;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis缓存实现类
 *
 * @author SoulBGM
 * @version 1.0
 * @date 2019/10/16 17:04
 */
public class RedisCache<K, V> implements Cache<K, V> {

    private RedisTemplate redisTemplate;

    private String keyPrefix = "session_cache:";

    /**
     * session 过期时间 默认60分钟
     */
    private long expiredTimeout = 60;

    public RedisCache(RedisTemplate redisTemplate) {
        if (redisTemplate == null) {
            throw new RuntimeException("RedisTemplate 不允许为null");
        }
        this.redisTemplate = redisTemplate;
    }

    public String getKeyPrefix() {
        return keyPrefix;
    }

    public void setKeyPrefix(String keyPrefix) {
        if (keyPrefix != null && !"".equals(keyPrefix)) {
            this.keyPrefix = keyPrefix;
        }
    }

    public long getExpiredTimeout() {
        return expiredTimeout;
    }

    public void setExpiredTimeout(long expiredTimeout) {
        this.expiredTimeout = expiredTimeout;
    }

    @Override
    public V get(K k) throws CacheException {
        Object val = redisTemplate.opsForValue().get(keyPrefix + k);
        /*if (val instanceof SimpleSession) {
            SimpleSession session = (SimpleSession) val;
            SimplePrincipalCollection simplePrincipalCollection = (SimplePrincipalCollection) session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY");
            if (simplePrincipalCollection != null) {
                System.out.println(simplePrincipalCollection.getRealmNames());
                System.out.println(simplePrincipalCollection.getPrimaryPrincipal());
            }

        }*/
        return val != null ? (V) val : null;
    }

    @Override
    public V put(K k, V v) throws CacheException {
        redisTemplate.opsForValue().set(keyPrefix + k, v,  expiredTimeout, TimeUnit.MINUTES);
        return v;
    }

    @Override
    public V remove(K k) throws CacheException {
        V v = get(k);
        redisTemplate.delete(keyPrefix + k);
        return v;
    }

    @Override
    public void clear() throws CacheException {
        redisTemplate.delete(keys());
    }

    @Override
    public int size() {
        return keys().size();
    }

    @Override
    public Set<K> keys() {
        Set keys = redisTemplate.keys(keyPrefix + "*");
        return keys != null ? keys : Collections.<K>emptySet();
    }

    @Override
    public Collection<V> values() {
        Set<K> keys = keys();
        Collection<V> v = new HashSet<>();
        for (K key : keys) {
            v.add((V) redisTemplate.opsForValue().get(key));
        }
        return v;
    }
}
